package com.ioudi.digu.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ioudi.digu.dao.support.IQuerySupport;
import com.ioudi.digu.service.support.ListResult;
import com.ioudi.digu.vo.MessagerPossess;

public class MessagerPossessDAO extends HibernateDaoSupport
{
	private IQuerySupport querySupport;
	
	public IQuerySupport getQuerySupport()
	{
		return querySupport;
	}

	public void setQuerySupport(IQuerySupport querySupport)
	{
		this.querySupport = querySupport;
	}

	public boolean saveOrUpdateMessager(MessagerPossess possess)
    {
	   	boolean result = false;
	   	if(possess == null)
	   	{
	   		return result;
	   	}
        Session s = this.getSession();
        try
        {
	       	s.beginTransaction();
	       	s.saveOrUpdate(possess);
	       	s.getTransaction().commit();
	       	result = true;
       }
       catch(Exception e)
       {
    	   s.getTransaction().rollback();
       }
       finally
       {
       		s.close();
       }
       return result;
   }
	
	public boolean deleteMessagerPossess(String[] possessIds)
	{
		boolean result = false;
        Session s = this.getSession();
        s.beginTransaction();
        try
        {
        	int i = possessIds.length - 1;
        	String sql = "delete MessagerPossess possess where possess.id=:id";
        	while(i >= 0)
        	{
        		s.createQuery(sql).setString("id", possessIds[i]).executeUpdate();
        	}
        	s.getTransaction().commit();
        	result = true;
        }
        catch(Exception e)
        {
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
        return result;
	}
	
	public MessagerPossess getPossess(String possessID)
    {
		MessagerPossess result = null;
	   	if(possessID == null)
	   	{
	   		return result;
	   	}
        Session s = this.getSession();
        try
        {
	       	s.beginTransaction();
	       	Object temp = s.createQuery("from MessagerPossess possess where possess.id=:possessID").
	       	    setString("possessID", possessID).uniqueResult();
	       	s.getTransaction().commit();
	       if(temp != null)
	       {
	    	   result = (MessagerPossess)temp;
	       }
        }
        catch(Exception e)
        {
    	   s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
       return result;
   }
	
	public List findPossessNoTotal(int index, int maxCount, Map<String, Object> condition)
	{
		String sql = "from MessagerPossess possess ";
		List<String> keys = new ArrayList<String>();
		String where = querySupport.createStatement("possess", condition, keys);
		sql += (where != null && where.length() > 0) ? " where " + where : "";
		sql += " order by possess.lastUse desc";
        Session s = this.getSession();
        s.beginTransaction();
        List result = null;
        try
        {
        	Query query  = s.createQuery(sql);
        	query = querySupport.createQuery(query, keys, condition);
        	query.setFirstResult(index);
        	if(maxCount > 0)
        	{
        		query.setMaxResults(maxCount);
        	}
        	result = query.list();
        	s.getTransaction().commit();
        }
        catch(Exception e)
        {
        	result = null;
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
		return result;
	}
	
	public ListResult findPossess(int index, int maxCount, Map<String, Object> condition)
	{
		if(condition == null)
		{
			condition = new HashMap<String, Object>();
		}
		if(!condition.keySet().contains("payFored"))
		{
			condition.put("payFored", true);
		}
		String sql = "from MessagerPossess possess ";
		List<String> keys = new ArrayList<String>();
		String where = querySupport.createStatement("possess", condition, keys);
		sql += (where != null && where.length() > 0) ? " where " + where : "";
		sql += " order by possess.lastUse desc";
        Session s = this.getSession();
        s.beginTransaction();
        ListResult result = new ListResult();
        try
        {
        	Query query = s.createQuery("select count(possess) " + sql);
        	Object temp = querySupport.createQuery(query, keys, condition).uniqueResult();
        	int total = 0;
        	if(temp != null)
        	{
        		total = ((Long)temp).intValue();
        	}
        	result.setTotal(total);
        	query = s.createQuery(sql);
        	query = querySupport.createQuery(query, keys, condition);
        	query.setFirstResult(index);
        	if(maxCount > 0)
        	{
        		query.setMaxResults(maxCount);
        	}
        	result.setContents(query.list());
        	s.getTransaction().commit();
        }
        catch(Exception e)
        {
        	result = null;
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
		return result;
	}
}
